package org.dfl.plugin;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
import org.dfl.plugin.views.DflPluginConsole;

/**
 * logger for log4j to show logs in a plugin console view
 * @author Piotr Wlodarczyk
 *
 */
public class EclipseConsoleLoggerAppender extends AppenderSkeleton {
	@Override
	protected void append(LoggingEvent arg0) {
		String msg = getLayout().format(arg0);

		if (arg0.getLevel().equals(Level.ERROR)
				|| arg0.getLevel().equals(Level.FATAL)) {
			DflPluginConsole.outputError(msg);
		} else if (arg0.getLevel().equals(Level.WARN)) {
			DflPluginConsole.outputWarning(msg);
		} else if (arg0.getLevel().equals(Level.INFO)) {
			DflPluginConsole.outputInfo(msg);
		} else {
			DflPluginConsole.outputMessage(msg);
		}
	}

	public void close() {
	}

	public boolean requiresLayout() {
		return true;
	}

	// private static MessageConsole getConsole() {
	// ConsolePlugin plugin = ConsolePlugin.getDefault();
	// IConsoleManager conMan = plugin.getConsoleManager();
	// IConsole[] existing = conMan.getConsoles();
	// for (int i = 0; i < existing.length; i++)
	// if (CONSOLE_NAME.equals(existing[i].getName()))
	// return (MessageConsole) existing[i];
	// //no console found, so create a new one
	// MessageConsole myConsole = new MessageConsole(CONSOLE_NAME, null);
	// conMan.addConsoles(new IConsole[] { myConsole });
	// return myConsole;
	// }
}
